<?php
/**
 * 管理画面コントローラー
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Manage_Admin extends  Controller {
	/*
	 * index
	 * @access  public
	 * @return  Response
	 */
	public function action_index() {
                //check access_token 
                
                
                $view = array();
                $view['tag']=1;
                if(isset($_COOKIE['access_token'])&& $_COOKIE['member_id'])
                {
                    $query = DB::select('id');
                    $query->from('bk_users');
                    $query->where('id',$_COOKIE['member_id']);
                    $query->where('access_token',$_COOKIE['access_token']);
                    $result = $query->execute()->as_array();
                    
                    if(count($result)==0)
                    {
                      $view['message'] = $_COOKIE['access_token'];
                       return Response::forge(View::forge('manage/admin/login',$view), D_HTTP_UPDATE);
                    }
                    // get list chuyên mục 
                    $query = DB::select('id','ct_name','alias','create_date','modified_date','user_id');
                    $query->from('bk_catagories');
                    $query->limit(10);
                    $result = $query->execute()->as_array();
                    $view['catagory'] = $result;
                    
                    
                }else{
                     $view['message']= 'vao';
                   return Response::forge(View::forge('manage/admin/login',$view), D_HTTP_UPDATE);
                }
              
                
                
            	return Response::forge(View::forge('manage/admin/index',$view), D_HTTP_UPDATE);
                
	}

	/*
	 * left
         * @access  public
	 * @return  Response
	 */
	public function action_login() {
                
		return Response::forge(View::forge('manage/admin/login'), D_HTTP_UPDATE);
	}
        public function action_submit()
        {
            $post = Input::post();
            $usr = isset($post['usr'])?$post['usr']:'';
            $pwd = isset($post['pwd'])?$post['pwd']:'';
            $query = DB::SELECT('id');
            $query ->from('bk_users');
            $query->where('user_name',$usr);
            $query->where('password',$pwd);
           
            $result = $query->execute()->as_array();
            $token = hash('sha256', time(), false);
            if(count($result)== 0)
            {
                $view['message']= $token;
                return Response::forge(View::forge('manage/admin/login',$view), D_HTTP_UPDATE);
            }
          
            
            setcookie('access_token',$token); 
            setcookie('member_id' ,$result[0]['id']);
            // this sets variables in the session 
           // $_SESSION['access_token']=$token; 
            //$_SESSION['member_id']=$result[0]['id']; 
            echo 'member id = '.$result['0']['id'];
            $query = DB::update('bk_users');
            $query->set(array( 
                'access_token' => $token,
                ));
            $query->where('id',$result['0']['id']);
            $query->execute();
            return Response::redirect('/manage/admin/index');
            
        } 
        /*
         * catagory 
         */
        public function action_catagory()
        {
            $post = Input::post();
            echo "action = " .isset($post['action']);
            
            
        }
        public function action_subcatagory()
        {
            $post = Input::post();
        }
        
	
	

}
